package com.weitu.worklicense.controller;

import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import com.weitu.worklicense.entity.GroundLicense;
import com.weitu.worklicense.entity.RadialLicense;
import com.weitu.worklicense.service.GroundLicenseService;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * Created by smp on 2017/6/9.
 */
@Controller
@RequestMapping(value = "workLicense/groundLicense")
public class WorkLicenseGroundController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "workLicense/groundLicense";
    private final GroundLicenseService groundLicenseService;
    private final AccessoryUtilService accessoryUtilService;

    @Autowired
    public WorkLicenseGroundController(GroundLicenseService groundLicenseService,AccessoryUtilService accessoryUtilService){
        this.accessoryUtilService=accessoryUtilService;
        this.groundLicenseService=groundLicenseService;

    }



    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }


    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request){
        Map<String,Object> paramMap=new HashMap<String,Object>();

        putParametersIntoMap(paramMap);

        int count=this.groundLicenseService.queryCount(paramMap);

        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,count);

        PageView pageView=dataGridPagination.getPageView();
        paramMap.put("pageView",pageView);

        List<GroundLicense> radialLicenseList=this.groundLicenseService.queryList(paramMap);
        return dataGridPagination.flushDataGridData(radialLicenseList);
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView add(){
        ModelAndView modelAndView=new ModelAndView();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("addUserId",BaseController.getSessionUser().getId());
        map.put("addUserName",BaseController.getSessionUser().getRealName());
        map.put("addDeptId",BaseController.getSessionUser().getDepartment().getId());
        map.put("addDeptName",BaseController.getSessionUser().getDepartment().getName());
        map.put("addTime", DateTimeUtil.formatterDateTime());
        modelAndView.addObject("userMessage",map);
        modelAndView.setViewName(MODULE_BATH_PATH+"/add");
        return modelAndView;
    }
    @SystemLog(module = "动土作业许可办理", methods = "新增动土作业许可信息")
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public AjaxResult addAjax(GroundLicense groundLicense){
        groundLicense.setId(IDGenerator.getId());
        groundLicense.setState(0);
        groundLicense.setDelState(0);
        groundLicense.setTicketNumber(DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS"));
        boolean flag=this.groundLicenseService.insertGroundLicense(groundLicense);
        if(flag){
            return AjaxResult.success("保存成功");
        }else {
            return AjaxResult.fail("保存失败");
        }
    }

    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView edit(String id){
        GroundLicense groundLicense=this.groundLicenseService.queryById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("groundLicense",groundLicense);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("uploadUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/edit");
        return modelAndView;
    }

    @SystemLog(module = "动土作业许可办理", methods = "编辑动土作业许可信息")
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public AjaxResult editAjax(GroundLicense groundLicense){
        groundLicense.setUpdateDeptId(BaseController.getSessionUser().getDepartment().getId());
        groundLicense.setUpdateUserId(BaseController.getSessionUser().getId());
        groundLicense.setUpdateTime(BaseController.getSessionUser().getUpdateTime());
        boolean flag=this.groundLicenseService.updateGroundLicense(groundLicense);
        if(flag){
            return AjaxResult.success("编辑成功");
        }else {
            return AjaxResult.fail("编辑失败");
        }
    }
    @SystemLog(module = "动土作业许可办理", methods = "删除动土作业许可信息")
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public AjaxResult delete(String id){
        int count=this.groundLicenseService.deleteGround(id);
        if(count==1){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.fail("删除失败");
        }
    }
    @ResponseBody
    @RequestMapping(value = "/execute",method = RequestMethod.POST)
    public AjaxResult executeAjax(String id){
        int count=this.groundLicenseService.execute(id);
        if (count> 0) {
            return success("执行成功！");
        } else {
            return fail("执行失败！");
        }
    }



    @RequestMapping(value = "/view",method = RequestMethod.GET)
    public ModelAndView view(String id){
        GroundLicense groundLicense=this.groundLicenseService.queryById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("groundLicense",groundLicense);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("uploadUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/view");
        return modelAndView;
    }
}
